//
//  AYHttpClient.m
//  SuiXin
//
//  Created by mac on 2023/10/9.
//

#import "AYHttpClient.h"
#import "AFNetworking.h"
#import "AFURLRequestSerialization.h"

#if defined(SQLITE_HAS_CODEC)
SQLITE_API int sqlite3_key(sqlite3 *db, const void *pKey, int nKey);
SQLITE_API int sqlite3_rekey(sqlite3 *db, const void *pKey, int nKey);
#endif

@implementation AYHttpClient

+ (void)post:(NSString *__nullable)urlString parameters:(NSDictionary *__nullable)params success:(void (^) (NSURLSessionDataTask *dataTask,id responseObjcet))success failure:(void (^)(NSURLSessionDataTask *dataTask,NSError *error))failure{
    Log(@"当前请求:%@\n,参数:%@",urlString,params);
    NSMutableDictionary *dic = [NSMutableDictionary dictionaryWithDictionary:params];
    if ([dic[@"isRegister"] isEqual:@"1"]) {
        [dic removeObjectForKey:@"isRegister"];
    }
    [[AYTool getManager] POST:urlString parameters:dic headers:[self getSignWithParams:params] progress:nil success:success failure:failure];
}

+ (void)post:(NSString *__nullable)urlString parameters:(NSDictionary *__nullable)params andFileParams:(NSDictionary *__nullable)fileParam andFileData:(NSData *)fileData success:(void (^) (NSURLSessionDataTask *dataTask,id responseObjcet))success failure:(void (^)(NSURLSessionDataTask *dataTask,NSError *error))failure {
    Log(@"当前请求:%@\n,参数:%@",urlString,params);
    [[AYTool getManager] POST:urlString parameters:params headers:[self getSignWithParams:params] constructingBodyWithBlock:^(id<AFMultipartFormData>  _Nonnull formData) {
        [formData appendPartWithFileData:fileData name:@"file" fileName:fileParam[@"fileName"] mimeType:fileParam[@"fileType"]];
    } progress:nil success:success failure:failure];
}

+ (void)get:(NSString *__nullable)urlString parameters:(NSDictionary *__nullable)params success:(void (^) (NSURLSessionDataTask *dataTask,id responseObject))success failure:(void (^)(NSURLSessionDataTask *dataTask,NSError *error))failure{
    Log(@"当前请求:%@\n,参数:%@",urlString,params);
    [[AYTool getManager] GET:urlString parameters:params headers:[self getSignWithParams:params] progress:nil success:success failure:failure];
}

+ (void)put:(NSString *__nullable)urlString tokenParams:(NSDictionary *__nullable)dic parameters:(NSDictionary *__nullable)params success:(void (^) (NSURLSessionDataTask *dataTask,id responseObject))success failure:(void (^)(NSURLSessionDataTask *dataTask,NSError *error))failure{
    Log(@"当前请求:%@\n,参数:%@",urlString,params);
    [[AYTool getManager] PUT:urlString parameters:params headers:@{} success:success failure:failure];
}

+ (void)getWithOutToken:(NSString *__nullable)urlString parameters:(NSDictionary *__nullable)params success:(void (^) (NSURLSessionDataTask *dataTask,id responseObject))success failure:(void (^)(NSURLSessionDataTask *dataTask,NSError *error))failure {
    Log(@"当前请求:%@\n,参数:%@",urlString,params);
    [[AYTool getManager] GET:urlString parameters:params headers:@{} progress:nil success:success failure:failure];
}

+ (void)deleteRequest:(NSString *__nullable)urlString tokenParams:(NSDictionary *__nullable)dic parameters:(NSDictionary *__nullable)params success:(void (^) (NSURLSessionDataTask *dataTask,id responseObject))success failure:(void (^)(NSURLSessionDataTask *dataTask,NSError *error))failure {
    Log(@"当前请求:%@\n,参数:%@",urlString,params);
    [[AYTool getManager] DELETE:urlString parameters:params headers:@{} success:success failure:failure];
}

+ (NSDictionary *)getSignWithParams:(NSDictionary *)dic {
    NSUUID *uuid = [NSUUID UUID];
    NSString *timeStamp = [AYTool getTimeNow];
    NSMutableDictionary *dic1 = [NSMutableDictionary dictionaryWithDictionary:dic];
    dic1[@"requestId"] = [uuid UUIDString];
    dic1[@"timeStamp"] = timeStamp;
    NSString *sign = [AYTool sortedDictionary:dic1];
    NSMutableDictionary *signDic = [NSMutableDictionary dictionaryWithDictionary:@{
        @"X-Device-Uuid": [AYTool getUUID],
        @"X-App-Version": APPVERSION,
        @"X-App-Package-Code": SXAPPBUNDLEID,
        @"X-Channel-Code": CHANNELCODE,
        @"X-Os-Type": @"iOS",
        @"X-Time-Stamp": timeStamp,
        @"X-Request-Id": [uuid UUIDString],
        @"X-Device-Manufacturer": [NSString stringWithFormat:@"apple-v-%@-v-%@",[AYTool getPhoneDeviceType], [UIDevice currentDevice].systemVersion]
    }];
    if (![dic[@"isRegister"] isEqual:@"1"]) {
        [signDic setValue:[AYTool md5String:sign] forKey:@"X-Api-Sign"];
    }
    return signDic;
}
@end
